
package com.be.beadmin.quality.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.dto.input.InspectionItemValueDto;
import com.be.beadmin.quality.dto.output.InspectionItemValueEx;
import com.be.beadmin.quality.entity.*;
import com.be.beadmin.quality.mapper.InspectionItemValueMapper;
import com.be.beadmin.quality.mapper.InspectionItemValueMapperEx;
import com.be.beadmin.quality.repository.InspectionItemValueRepository;
import com.be.beadmin.quality.repository.TemplateInspectionItemRepository;
import com.be.beadmin.quality.service.InspectionItemValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-05-28
**/
@Service
@RequiredArgsConstructor
public class InspectionItemValueServiceImpl implements InspectionItemValueService {

    @Autowired
    private  InspectionItemValueRepository inspectionItemValueRepository;
    @Autowired
    private  InspectionItemValueMapper inspectionItemValueMapper;
    @Autowired
    private  InspectionItemValueMapperEx inspectionItemValueExMapper;

    @Autowired
    private TemplateInspectionItemRepository templateInspectionItemRepository;

//    @Autowired
//    private  InspectionItemValueMapperEx inspectionItemValueExMapper;

    @Override
    public Page<InspectionItemValueEx> getInspectionItemValueByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<InspectionItemValue> page = inspectionItemValueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(InspectionItemValue_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(InspectionItemValue_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<InspectionItemValueEx> page1 = page.map(inspectionItemValueExMapper::toEntity);
            return page1;

    }

    @Override
    public void addInspectionItemValue(InspectionItemValueDto inspectionItemValueDto) {
            InspectionItemValue result = inspectionItemValueRepository.findInspectionItemValueByIdAndDelete(inspectionItemValueDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("InspectionItemValue不存在,请重新查询");
            }
            InspectionItemValue inspectionItemValue = inspectionItemValueMapper.toEntity(inspectionItemValueDto);
            inspectionItemValueRepository.save(inspectionItemValue);
    }

    @Override
    public void updateInspectionItemValue(InspectionItemValueDto inspectionItemValueDto) {
            InspectionItemValue inspectionItemValue = inspectionItemValueRepository.findInspectionItemValueById(inspectionItemValueDto.getId());
            inspectionItemValue = inspectionItemValueMapper.toEntity(inspectionItemValueDto);
            inspectionItemValueRepository.save(inspectionItemValue);
    }


    @Override
    public void delInspectionItemValue(String id) {
        InspectionItemValue inspectionItemValue = inspectionItemValueRepository.findInspectionItemValueById(id);
        inspectionItemValue.setDelete(1);
        inspectionItemValueRepository.save(inspectionItemValue);
    }

    @Override
    public void delInspectionItemValues (List<String> ids) { }

    /**
     * 生成检验记录表检验项目值
     *
     * @param save
     * @param template
     */
    @Override
    public void generateInspectionItemValue(InspectionTask save, InspectionTemplate template) {
        for (TemplateInspectionItem templateInspectionItem : templateInspectionItemRepository.findTemplateInspectionItemByInspectionTemplateIdAndDelete(template.getId(), 0)) {
            InspectionItemValue inspectionItemValue = new InspectionItemValue();
            inspectionItemValue.setInspectionTaskId(save.getId());
            inspectionItemValue.setTemplateInspectionItemId(templateInspectionItem.getId());
//            inspectionItemValue.set(templateInspectionItem.getId());
            inspectionItemValueRepository.save(inspectionItemValue);
        }

    }
}